package com.example.demo.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.demo.common.entity.User;
import com.example.demo.common.service.UserService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.ObjDoubleConsumer;

@Controller
@RestController
@RequestMapping("/user")
public class UserWeb1 {

    private final String crud="/user/UserCrud";  //增删改查页面

    @Autowired
    UserService userService;

    @RequestMapping("/getuser")
    public Map<String,Object> getuser(int page, int rows,String filter,String sort,String order){
        /*System.out.println("page="+page+"    rows="+rows+"   filter="+filter+"   "+sort+" "+ order);
        List<User> lists=userService.selectAll();
        
        int total=userService.selectCount(new EntityWrapper<>());
        //分页
        Page<Map<String,Object>> pg=new Page(page,rows);
        lists=userService.selectListByFilter(filter,sort,order,pg);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("total",total);
        map.put("rows",lists);*/
        Map<String,Object> map=new HashMap<String,Object>();
        return map;
    }
    
    @RequestMapping("/getusers")
    public List<User> getusers(){
        /*List<User> lists=userService.selectAll();
        Page<User> pg=new Page(1,2);
        pg.setOrderByField("ac is null,username");
        lists=userService.selectListByFilter("","","",pg);*/
       

        /*EntityWrapper<User> entityWrapper=new EntityWrapper<User>();

        Page<User> pg=new Page(1,2);
        System.out.println(userService.selectRealName()+"----");
        */
        //List<User> lists=userService.selectList(entityWrapper);
        return null;
    }
    
    @RequestMapping("/login")
    public String login() {
    	String username="123";
    	String password="123456";
        // 从SecurityUtils里边创建一个 subject
        Subject subject = SecurityUtils.getSubject();
        // 在认证提交前准备 token（令牌）
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        // 执行认证登陆
        System.out.println(subject.isPermitted("123:456")+"  是否拥有权限");
        try {
            subject.login(token);

            //根据权限，指定返回数据
            String role = "manager";
            if (subject.hasRole(role)) {
                return "欢迎登陆";
            }
            if ("admin".equals(role)) {
                return "欢迎来到管理员页面";
            } 
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
        return ("用户名或者密码错误");
    }

    @RequestMapping("/add")   //新增
    public String add(Model model,@RequestBody User user){
        System.out.println(user);
        userService.insert(user);
        return "success";
    }

    @RequestMapping("/del")  //删除
    public String del(String id){
        userService.deleteById(id);
        return "sucess";
    }
    @RequestMapping("/update")  //修改
    public String update(Model model,@RequestBody User user){
        System.out.println(user);
        userService.updateById(user);
        return "sucess";
    }
}
